{% extends 'admin/master.html' %}
{% import 'admin/lib.html' as lib with context %}
{% import 'admin/static.html' as admin_static with context %}
{% import 'admin/model/layout.html' as model_layout with context %}
{% import 'admin/actions.html' as actionlib with context %}
{% import 'admin/model/row_actions.html' as row_actions with context %}

{% block head %}
    {{ super() }}
    {{ lib.form_css() }}
{% endblock %}

{% block body %}
    <section class="content-header">
        <h1>{{ admin_view.name }} {% if count %} ({{ count }}){% endif %}</h1>
    </section>
    <section class="content">
        <div class="row">
            <div class="col-xs-12">
                <div class="box">
                    <div class="box-header with-border">
                        {% block model_menu_bar %}
                            {% if admin_view.can_create %}
                                {# todo: need refactor here#}
                                {%- if admin_view.create_modal -%}
                                    {{ lib.add_modal_button(url=get_url('.create_view', url=return_url, modal=True), title=_gettext('Create New Record'), content=_gettext('Create'), btn_class='btn btn-default btn-sm') }}
                                {% else %}
                                    <a href="{{ get_url('.create_view', url=return_url) }}"
                                       title="{{ _gettext('Create New Record') }}" class="btn btn-default btn-sm">
                                        {{ _gettext('Create') }}
                                    </a>
                                {%- endif -%}
                            {% endif %}

                            {% if admin_view.can_export %}
                                {{ model_layout.export_options() }}
                            {% endif %}

                            {% block model_menu_bar_before_filters %}{% endblock %}

                            {% if filters %}
                                {{ model_layout.filter_options() }}
                            {% endif %}

                            {% if can_set_page_size %}
                                {# todo: need refactor here#}
                                {{ model_layout.page_size_form(page_size_url) }}
                            {% endif %}

                            {% if actions %}
                                {{ actionlib.dropdown(actions) }}
                            {% endif %}

                            {% if search_supported %}
                                {{ model_layout.search_form() }}
                            {% endif %}
                            {% block model_menu_bar_after_filters %}{% endblock %}
                        {% endblock %}
                    </div>
                    <div class="box-body">
                        {% if filters %}
                            {{ model_layout.filter_form() }}
                            <div class="clearfix"></div>
                        {% endif %}

                        {% block model_list_table %}
                            <div class="table-responsive">
                                {% for table in tables %}
                                    {{ table|safe }}
                                {% endfor %}
                            </div>
                        {% endblock %}
                    </div>
                </div>
                {% block actions %}
                    {{ actionlib.form(actions, get_url('.action_view')) }}
                {% endblock %}

                {%- if admin_view.edit_modal or admin_view.create_modal or admin_view.details_modal -%}
                    {{ lib.add_modal_window() }}
                {%- endif -%}
            </div>
        </div>
    </section>
{% endblock %}

{% block tail %}
    {{ super() }}
    <script src="{{ admin_static.url(filename='admin/js/filters.js', v='1.0.0') }}"></script>
    {{ lib.form_js() }}

    {{ actionlib.script(_gettext('Please select at least one record.'),
                        actions,
                        actions_confirmation) }}

    <script language="javascript">
        (function ($) {
            $('[data-role=tooltip]').tooltip({
                html: true,
                placement: 'bottom'
            });
            {% if filter_groups %}
                var filter = new AdminFilters(
                    '#filter_form', '.field-filters',
                    {{ filter_groups|tojson|safe }},
                    {{ active_filters|tojson|safe }}
                );
            {% endif %}
        })(jQuery);

        // Catch exception when closing dialog with <esc> key
        // and prevent accidental deletions.
        function safeConfirm(msg) {
            try {
                var isconfirmed = confirm(msg);
                if (isconfirmed == true) {
                    return true;
                }
                else {
                    return false;
                }
            }
            catch (err) {
                return false;
            }
        }
    </script>
{% endblock %}
